﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcHtmlDemo.Controllers
{
    public class DemoController: Controller
    {
        private List<Product> _list = new List<Product>()
        {
            new Product {Title = "A", Value = 1},
            new Product {Title = "B", Value = 2},
            new Product {Title = "C", Value = 3},
            new Product {Title = "D", Value = 4},
        };

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult ProductList()
        {
          
            return Json(_list, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交新数据
        /// </summary>
        /// <param name="title"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Submit(string title, int value)
        {
            var dict = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(title))
            {
                _list.Add(new Product { Title = title, Value = value});
                dict.Add("ok", true);
                return Json(dict);
            }
            dict.Add("ok", false);
            return Json(dict);
        }


        public ActionResult Submit2(Product p)
        {
            var dict = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(p.Title))
            {
                _list.Add(p);
                dict.Add("ok", true);
                return Json(dict);
            }
            dict.Add("ok", false);
            return Json(dict);
        }


    }

    /// <summary>
    /// 产品
    /// </summary>
    public class Product
    {
        public string Title { get; set; }

        public int Value { get; set; }
    }
}